package com.rcadmin.busi.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import com.rcadmin.busi.admin.ddl.SysParam;
import com.rcadmin.busi.web.contant.GroupType;
import com.rcadmin.busi.web.ddl.WebTemplateGroupkey;
import com.rcadmin.busi.web.dto.WebTemplateGroupkeyDto;
import com.rcadmin.busi.web.service.GroupKeyService;
import com.rcadmin.busi.web.service.TemplatePackageService;
import com.rcadmin.common.contant.SysParamCode;
import com.rcadmin.core.app.session.SessionException;
import com.rcadmin.core.jpa.util.PageBean;
import com.rcadmin.core.web.BaseAction;

/**
 * 网站资源分組展示类
 * 
 * @author tanson lam
 * 
 */
@Controller
public class GroupKeyController extends BaseAction {

	private static final String NAVLIST_PAGE = "/web/groupkey/navlist";
	private static final String BANNERLIST_PAGE = "/web/groupkey/bannerlist";
	private static final String NAV_FORM = "/web/groupkey/navform";
	private static final String BANNER_FORM = "/web/groupkey/bannerform";
	@Resource
	private TemplatePackageService templatePackageService;
	@Resource
	private GroupKeyService groupKeyService;

	/**
	 * 查詢导航分類列表
	 * 
	 * @throws SessionException
	 */
	@GetMapping("/groupkey/showNav.op")
	public ModelAndView showNav() {
		Map<String, Object> dataModel = new HashMap<String, Object>();
		String condition = "group_type ='" + GroupType.NAV.getType()
				+ "' and user_id=" + getLoginUserId() + bulidConditionSql();
		PageBean<WebTemplateGroupkeyDto> pageBean = groupKeyService.pageQuery(
				condition, bulidOrderBySql(), getPageSize(), getPage());
		dataModel.put("templatePackageCode", templatePackageService
				.findTemplatePackageParam(getLoginUserId()));
		List<SysParam> groupType = GroupType.ofSystemParam();
		dataModel.put("groupType", groupType);
		dataModel.put("iseffCode",
				sysParamService.getSysParamByRoot(SysParamCode.ISEFF));
		pageBean.setPageUrl(getPaginationUrl("/template/showNav.op"));
		dataModel.put("pageBean", pageBean);
		return render(NAVLIST_PAGE, dataModel);
	}

	/**
	 * 查詢栏目分類列表
	 * 
	 * @throws SessionException
	 */
	@GetMapping("/groupkey/showBanner.op")
	public ModelAndView showBanner() {
		Map<String, Object> dataModel = new HashMap<String, Object>();
		String condition = "group_type ='" + GroupType.BANNER.getType()
				+ "' and user_id=" + getLoginUserId() + bulidConditionSql();
		PageBean<WebTemplateGroupkeyDto> pageBean = groupKeyService.pageQuery(
				condition, bulidOrderBySql(), getPageSize(), getPage());
		List<SysParam> groupType = GroupType.ofSystemParam();
		dataModel.put("groupType", groupType);
		dataModel.put("templatePackageCode", templatePackageService
				.findTemplatePackageParam(getLoginUserId()));
		dataModel.put("iseffCode",
				sysParamService.getSysParamByRoot(SysParamCode.ISEFF));
		pageBean.setPageUrl(getPaginationUrl("/template/showBanner.op"));
		dataModel.put("pageBean", pageBean);
		return render(BANNERLIST_PAGE, dataModel);
	}

	/**
	 * 显示编辑导航表单
	 */
	@GetMapping("/groupkey/showEditNavForm.op")
	public ModelAndView showEditNavForm() {
		Map<String, Object> dataModel = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (!StringUtils.isEmpty(id)) {
			WebTemplateGroupkey webGroup = groupKeyService.findOne(
					getLoginUserId(), Integer.valueOf(id));
			dataModel.put("bean", webGroup);
		}
		List<SysParam> iseffCode = sysParamService
				.getSysParamByRoot(SysParamCode.ISEFF);
		List<SysParam> groupType = GroupType.ofSystemParam(GroupType.NAV);
		dataModel.put("templatePackageCode", templatePackageService
				.findTemplatePackageParam(getLoginUserId()));
		dataModel.put("iseffCode", iseffCode);
		dataModel.put("groupType", groupType);
		return render(NAV_FORM, dataModel);
	}

	/**
	 * 显示编辑栏目表单
	 */
	@GetMapping("/groupkey/showEditBannerForm.op")
	public ModelAndView showEditBannerForm() {
		Map<String, Object> dataModel = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (!StringUtils.isEmpty(id)) {
			WebTemplateGroupkey webGroup = groupKeyService.findOne(
					getLoginUserId(), Integer.valueOf(id));
			dataModel.put("bean", webGroup);
		}
		List<SysParam> iseffCode = sysParamService
				.getSysParamByRoot(SysParamCode.ISEFF);
		List<SysParam> groupType = GroupType.ofSystemParam(GroupType.BANNER);
		dataModel.put("templatePackageCode", templatePackageService
				.findTemplatePackageParam(getLoginUserId()));
		dataModel.put("iseffCode", iseffCode);
		dataModel.put("groupType", groupType);
		return render(BANNER_FORM, dataModel);
	}

}
